﻿using UnityEngine;

namespace UIFramework
{
    /// <summary>
    /// 单例基类 无需 也不要 手动部署 直接Instance
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Singleton<T> : MonoBehaviour where T : Singleton<T>
    {
        private static T instance = null;

        public static T Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = FindObjectOfType<T>();
                    if (instance == null)
                    {
                        var gameObject = new GameObject();
                        DontDestroyOnLoad(gameObject);
                        gameObject.AddComponent<T>();
                        Debug.Log($"实例化单例对象-{gameObject.name}");
                    }
                }
                return instance;
            }
        }
    }
}